<?php
namespace app\super\controller;
use think\Db;
use think\Controller;
use  think\Request;


class System extends Base
{
	public function Index(){
		
		 return view();
	}
	public function loginlog(){

		return view();
	}
	public function getloginloglist(){
		$where=" 1=1 ";

		if($_POST["stime"]&&$_POST["etime"]){
			$stime = $_POST["stime"]." 00:00:00";
			$etime = $_POST["etime"]." 23:59:59";
			$where=$where." and createtime between '".$stime."' and '".$etime."'";
		}


		$count =db('loginlog')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('loginlog')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		foreach ($list as $key => &$value) {
			
			$json = json_decode($value["address"],true);
			$value["address"]= $json["data"]["prov"]."-".$json["data"]["city"]."-".$json["data"]["area"];
		}


		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	public function loginlogdel(){
		$request = Request::instance();
		$id =$request->post('id');
			$where = 'id='.$id;
		   $list=db('loginlog')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
	}
	function pic(){

		return view();
	}
	function addpic(){
		
		$request = Request::instance();
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["typeid"] =$request->post('typeid');
				$data["imgurl"] =$request->post('imgurl');
				$data["state"] =$request->post('state');
				
			if($data["id"]){
				if(db('sys_pic')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
			
			  if(db('sys_pic')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('sys_pic')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			
		}
		
		return view();
	}
	 public function getpiclist(){
		$where="";
		$list = db('sys_pic')->where($where)->select();
		

		return json($list);
		
	}
	
	function delpic(){
		$request = Request::instance();
		$id =$request->post('id');
		$where = 'id='.$id;
		   $list=db('sys_pic')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}

	public function config(){
		$mid="0";
		$request = Request::instance();
        if($request->isPost()){
          
			$data["kefuphone"]=$request->post('kefuphone');
			$data["model1"]=$request->post('model1');
			$data["model2"]=$request->post('model2');
			$data["push"]=$request->post('push');
			$data["cdn"]=$request->post('cdn');
			$data["apikey"]=$request->post('apikey');
			$data["playapikey"]=$request->post('playapikey');
           
            if(db('sys_webconfig')->where('mid',$mid)->update($data)){
					
                return ['status'=>200,'message'=>'修改成功'];
          }else{
               return ['status'=>500,'message'=>'修改失败'];
          }
        }else{
            


            $info = db("sys_webconfig")->where("mid=".$mid)->find();
            if($info){
				$this->assign('info',$info);
				
            }else{
                $data["mid"]=$mid;
                db('sys_webconfig')->insertGetId($data);


            }
           
            return view();

        }
      
	}

	public function feedback(){

		$request = Request::instance();
        if($request->isPost()){
			$where="";
			$count =db('feedback')->where($where)->count();
			$first = $_POST["size"] * ($_POST["page"] -1);
			 
			$list = db('feedback')->join("mechanism","mechanism.id = feedback.mid")->join("users","users.id= feedback.userid")->where($where)->field("feedback.*,mechanism.name,users.phone")->order("id desc")->limit($first,$_GET["size"])->select();
	
			$obj["count"]=(int)($count/$_POST["size"])+1;
			$obj["allcount"]=$count;
			$obj["list"]=$list;
			return json($obj);

		}else{

			return view();
		}
	}
	function delfeedback(){
		$request = Request::instance();
		$id =$request->post('id');
		$where = 'id='.$id;
		   $list=db('feedback')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	function loginloglistdel(){
		$request = Request::instance();
		$stime = $_POST["stime"]." 00:00:00";
		$etime = $_POST["etime"]." 23:59:59";
		
		$where=" createtime between '".$stime."' and '".$etime."'";
		   $list=db('loginlog')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	function video(){

		return view();
	}
	function getvideo(){
		$request = Request::instance();
		$where="1=1";

		if($_POST["dailiid"]){
			$where=$where." and dailiid=".$_POST["dailiid"];
		}
		
	
		$count =db('video')->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		$sys_webconfig = db("sys_webconfig")->where("mid=0")->find();
		$list = db('video')->where($where)->order("id desc")->limit($first,$_POST["size"])->select();
		foreach ($list as $key => &$value) {
			$value["code"]=$value["videourl"];
			$value["rtmp"]=getPushUrl($sys_webconfig["push"], $value["videourl"], $sys_webconfig["apikey"], "2050-01-01 23:59:59");
			// $play = getPlayUrl($sys_webconfig["cdn"], $value["videourl"]);
			// $value["url"]=$play[0];
			$time = date("Y-m-d H:i:s",time()+7200);
			$play = getplayurl2($sys_webconfig["cdn"], $value["videourl"], $sys_webconfig["playapikey"], $time);
			$value["m3u8"]=$play[0];

			if($value["dailiid"]){
				$daili = db('daili')->where("id=".$value["dailiid"])->find();
				$value["dailiname"]=$daili["name"];
			}else{
				$value["dailiname"]="自营";
			}
			if($value["mid"]){
				$mechanism = db('mechanism')->where("id=".$value["mid"])->find();
				$value["jigouname"]=$mechanism["name"];
			}else{
				$value["mid"]="";
			}
		}
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}

}